/*
 Darts -- Double-ARray Trie System

 $Id: darts.cpp 1674 2008-03-22 11:21:34Z taku $;

 Copyright(C) 2001-2007 Taku Kudo <taku@chasen.org>
 All rights reserved.
 */
#include "g2p_dsearch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>

Darts::DoubleArray thai_da;
Darts::DoubleArray eng_da;
Darts::DoubleArray acronyms_da;
Darts::DoubleArray pronun_da;
Darts::DoubleArray dict_esan_da;

pronun_word thai;
pronun_word eng;
pronun_word acronyms;
pronun_word pronun;

pronun_phone_set  dict_esan_phone;
/*
 * Genaral Function for Dict Load
 */
int Dict_Load_Index(char * file_tree,Darts::DoubleArray * dict_tree){
	//dict_phone = (pronun_word *) malloc(sizeof(pronun_word));
	std::string index = file_tree;
	if (dict_tree->open(index.c_str())) {
		std::cerr << "Error: cannot open " << index << std::endl;
		return -1; // Load Error.
	}
	return 1; //Load Succeed
}
int Dict_Genaral_Load(char * file_tree,char * file_phone,Darts::DoubleArray * dict_tree,pronun_phone_set * dict_phone,int dsize){
	//dict_phone = (pronun_word *) malloc(sizeof(pronun_word));
	//load tree
	std::string index = file_tree;
	if (dict_tree->open(index.c_str())) {
		std::cerr << "Error: cannot open " << index << std::endl;
		return -1; // Load Error.
	}

	//load phoneme data
	FILE * fp = fopen(file_phone, "r");
	int count = 0;
	int phone_tmp[20];
	int phone_size = 0;
	int current_row = 0;

	while (!feof(fp)) {
		phone_src_struct st;
		fread(&st,sizeof(phone_src_struct),1,fp);
		if(count==0){
			phone_tmp[phone_size++] = st.number;
		}else if(st.prefix=='B'){
			//add new phone

			pronun_phone * pp = (pronun_phone *)malloc(sizeof(pronun_phone));
			pp->n_syl = phone_size;
			pp->phone = (int *)calloc(phone_size,sizeof(int));
			for(int i=0;i<phone_size;i++){
				pp->phone[i] = phone_tmp[i];
			}

			dict_phone->phone[current_row++] = pp;
			//clear
			phone_size = 0;
			phone_tmp[phone_size++] = st.number;
		}else if(st.prefix=='I'){
			//append tmp_phone
			phone_tmp[phone_size++] = st.number;
		}
		count++;
	}

	dict_phone->size = current_row;
	//dict_phone->phone = phoneme;
	//dict_phone->word = word;
	fclose(fp);
	return 1; //Load Succeed
}
pronun_phone * Dict_Genaral_Search(Darts::DoubleArray *dict_tree,pronun_phone_set * dict_phone,char * word){
	//printf("\nSearch");

		int re ;
		dict_tree->exactMatchSearch(word,re);
		if(re!=-1){
			return dict_phone->phone[re];

		}else
			return NULL;

	/*
	Darts::DoubleArray::result_pair_type result_pair[1024];
	char * a = (char *) calloc(strlen(word) + 1, sizeof(char));
	strcpy(a, word);
	size_t num = dict_tree->commonPrefixSearch(a, result_pair, sizeof(result_pair));
	if (num == 0) {
		//printf("Find NULL!");
		free(a);
		return NULL;
	} else {
		printf("Find OK!");
		if (strcmp(dict_phone->word[result_pair[num - 1].value], a) == 0) {
		free(a);
			return dict_phone->phone[result_pair[num - 1].value];
		} else {
			free(a);
		return NULL;
		}

	}*/

}
int Dict_Esan_Load(char * file_tree, char *file_phoneme){
	pronun_phone **phone = (pronun_phone **)calloc(sizeof(pronun_phone *),THAI_DICT_SIZE);
	dict_esan_phone.phone = phone;
	int r = Dict_Genaral_Load(file_tree,file_phoneme,&dict_esan_da,&dict_esan_phone,THAI_DICT_SIZE);
	//printf("\nDICT LOAD :%d",r);
	return 0;
}

pronun_phone * Dict_Esan_Search(char * word) {
	return Dict_Genaral_Search(&dict_esan_da,&dict_esan_phone,word);
}
/*
int Dict_Thai_Load(char * file_tree, char *file_phoneme){
	int r = 0;//Dict_Genaral_Load(file_tree,file_phoneme,&thai_da,&thai,THAI_DICT_SIZE);
	//printf("\nDICT LOAD :%d",r);
	return r;
}
int Dict_Eng_Load(char * file_tree, char *file_phoneme){
	int r = 0;//Dict_Genaral_Load(file_tree,file_phoneme,&eng_da,&eng,ENG_DICT_SIZE);
	return r;
}
int Dict_Acronyms_Load(char * file_tree, char *file_phoneme){
	int r = 0;// Dict_Genaral_Load(file_tree,file_phoneme,&acronyms_da,&acronyms,ACRONYMS_DICT_SIZE);
	return r;
}
int Dict_Pronun_Load(char * file_tree, char *file_phoneme){
	int r = 0; // Dict_Genaral_Load(file_tree,file_phoneme,&pronun_da,&pronun,PRONUN_DICT_SIZE);
	//printf("\nDICT LOAD :%d",r);
	return r;
}
*/
/*
int dict_thai_load(char * file_tree, char *file_phoneme) {
	thai = (pronun_word *) malloc(sizeof(pronun_word));
	std::string index = file_tree;
	if (thai_da.open(index.c_str())) {
		std::cerr << "Error: cannot open " << index << std::endl;
		return -1;
	}
	char ** thai_phoneme = (char**) calloc(THAI_DICT_SIZE, sizeof(char *));
	char ** thai_word = (char**) calloc(THAI_DICT_SIZE, sizeof(char *));

	FILE * fp = fopen(file_phoneme, "r");
	int count = 0;
	while (!feof(fp)) {
		char s[512] = "";
		char s2[512] = "";
		fscanf(fp, "%s\t%s\n", s2, s);
		//printf("\n%s",s);
		char * p1 = (char *) calloc(strlen(s) + 1, sizeof(char));
		char * p2 = (char *) calloc(strlen(s2) + 1, sizeof(char));
		strcpy(p1, s);
		strcpy(p2, s2);
		thai_phoneme[count] = p1;
		thai_word[count] = p2;
		count++;
	}
	thai->size = count;
	thai->phone = thai_phoneme;
	thai->word = thai_word;
	return 0;
}*/
/*
char * Dict_Thai_Search(char * word) {
	return Dict_Genaral_Search(&thai_da,&thai,word);
}
char * Dict_Eng_Search(char * word) {
	return Dict_Genaral_Search(&eng_da,&eng,word);
}
char * Dict_Pronun_Search(char * word) {
	return Dict_Genaral_Search(&pronun_da,&pronun,word);
}
char * Dict_Acronyms_Search(char * word) {
	return Dict_Genaral_Search(&acronyms_da,&acronyms,word);
}
*/
int main222(int argc, char **argv) {
	if (argc < 3) {
		std::cerr << "Usage: " << argv[0] << " <Index File> <Test File>"
				<< std::endl;
		return -1;
	}
	Darts::DoubleArray da;
	std::string index = argv[argc - 2];
	char * test = argv[argc - 1];
	if (da.open(index.c_str())) {
		std::cerr << "Error: cannot open " << index << std::endl;
		return -1;
	}

	Darts::DoubleArray::result_pair_type result_pair[1024];
	Darts::DoubleArray::key_type key[1024];

	char * temp_str = (char *) calloc(1024, sizeof(char));
	FILE * fp = fopen(test, "r");

	if (fp == NULL) {
		std::cout << "Test Word" << std::endl;
		exit(0);
	}
	while (!feof(fp)) {
		fscanf(fp, "%s\n", temp_str);
		size_t num = da.commonPrefixSearch(temp_str, result_pair,
				sizeof(result_pair));
		if (num == 0) {
			std::cout << temp_str << ": not found" << std::endl;
		} else {
			std::cout << temp_str << ": found, num=" << num << " ";
			for (size_t i = 0; i < num; ++i) {
				std::cout << " " << result_pair[i].value << ":"
						<< result_pair[i].length;
			}
			std::cout << std::endl;
		}
	}

	return 0;
}
